package com.yfuxdeer.youchat.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.pojo.FragmentItem;
import com.yfuxdeer.youchat.ui.RoundImageView;
import com.yfuxdeer.youchat.utils.ImageUtil;

import java.util.List;

/**
 * 主页显示消息列表的数据适配器
 */
public class FragmentListAdapter extends BaseAdapter {

    private final static String TAG = FragmentListAdapter.class.getSimpleName();
    private List<FragmentItem> fragmentItemList;
    private Context context;

    public FragmentListAdapter(List<FragmentItem> fragmentItemList, Context context) {
        this.fragmentItemList = fragmentItemList;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (fragmentItemList == null) return 0;
        return fragmentItemList.size();
    }

    @Override
    public Object getItem(int i) {
        return fragmentItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = View.inflate(context, R.layout.fragment_item, null);
            holder = new ViewHolder();
            holder.head = view.findViewById(R.id.head);
            holder.title = view.findViewById(R.id.main_title);
            holder.sub_title = view.findViewById(R.id.sub_title);
            holder.addition = view.findViewById(R.id.addition);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        FragmentItem fragmentItem = fragmentItemList.get(i);
        Log.e(TAG, "getView: " + fragmentItem);
        ImageUtil.glideLoad(context, fragmentItem.getHead_id(), 0, holder.head);
        holder.title.setText(fragmentItem.getTitle());
        holder.sub_title.setText(fragmentItem.getSub_title());
        holder.addition.setText(fragmentItem.getAddition());
        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public static class ViewHolder {
        public RoundImageView head;
        public TextView title;
        public TextView sub_title;
        public TextView addition;
    }
}